package com.hdaccp.shop.controller;

import com.hdaccp.pojo.po.Shop;

import com.hdaccp.shop.dao.ShopDao;

import com.hdaccp.shop.service.ShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private ShopDao shopDao;
@Autowired
private ShopService shopService;
    @RequestMapping("/query")
    public List<Shop> query(){
        return shopDao.findAll();
        
    }
    @RequestMapping("/getOne/{shopId}")
    public Shop getOne(@PathVariable("shopId") int shopId){
        return shopDao.findById(shopId).get(); 
    }
    @RequestMapping("/queryPage/{cpage}/{pagesize}")
    public List<Shop> queryPage(@PathVariable("cpage") int cpage,@PathVariable("pagesize") int pagesize){
        return shopService.queryPage(cpage,pagesize );
        }
    @RequestMapping("/save/{shopname}/{shopmoney}/{shopimg}/{shoptypeId}/{shopcailiaoId}/{cailiao}/{baozhuang}/{huayu}/{fusong}")
    public String save(@PathVariable("shopname") String shopname,@PathVariable("shopmoney") double shopmoney,@PathVariable("shopimg")
    String shopimg,@PathVariable("shoptypeId") int shoptypeId,@PathVariable("shopcailiaoId") int shopcailiaoId,@PathVariable("cailiao") String cailiao,@PathVariable("baozhuang") String baozhuang,@PathVariable("huayu") String huayu,@PathVariable("fusong") String fusong){
       String message="成功了";
        Shop shop=new Shop();
        shop.setShopname(shopname);
        shop.setShopmoney(shopmoney);
        shop.setShopimg(shopimg);
        shop.setShoptypeId(shoptypeId);
        shop.setShopcailiaoId(shopcailiaoId);
        shop.setCailiao(cailiao);
        shop.setBaozhuang(baozhuang);
        shop.setHuayu(huayu);
        shop.setFusong(fusong);
        try {
            shopDao.save(shop);
        } catch (Exception e){
            message="失败了";
        }

return message;
    }


    @RequestMapping("/updata/{shopId}/{shopname}/{shopmoney}/{shopimg}/{shoptypeId}/{shopcailiaoId}/{cailiao}/{baozhuang}/{huayu}/{fusong}")
    public String updata(@PathVariable("shopId") int shopId,@PathVariable("shopname") String shopname,@PathVariable("shopmoney") double shopmoney,@PathVariable("shopimg")
            String shopimg,@PathVariable("shoptypeId") int shoptypeId,@PathVariable("shopcailiaoId") int shopcailiaoId,@PathVariable("cailiao") String cailiao,@PathVariable("baozhuang") String baozhuang,@PathVariable("huayu") String huayu,@PathVariable("fusong") String fusong){
        String message="修改成功";
        Shop shop= shopDao.findById(shopId).get();


        shop.setShopname(shopname);
        shop.setShopmoney(shopmoney);
        shop.setShopimg(shopimg);
        shop.setShoptypeId(shoptypeId);
        shop.setShopcailiaoId(shopcailiaoId);
        shop.setCailiao(cailiao);
        shop.setBaozhuang(baozhuang);
        shop.setHuayu(huayu);
        shop.setFusong(fusong);
        try {
            shopDao.save(shop);
        } catch (Exception e){
            message="失败了";
        }


        return message;
    }
    @RequestMapping("/del/{shopId}")
    public String del(@PathVariable int shopId){
        String message="成功了";

        try {  

        shopDao.deleteById(shopId);
        } catch (Exception e){
            message="失败了";
        }


        return message;
    }
    
    
    
}
